﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ShortcutExecutor.cs" company="TechBits">
//   Copyright (c) TechBits. All rights reserved.
// </copyright>
// <author>
//   Leandro G Vacirca.
// </author>
// <summary>
//   Defines the ShortcutExecutor type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace IntelliShortCut.Core
{
    using System;
    using System.Diagnostics;
    using System.Threading;
    
    using Data;

    /// <summary>
    /// Enables to execute an application associated to a shorcut.
    /// </summary>
    public class ShortcutExecutor
    {
        #region "Public Methods"

        /// <summary>
        /// Executes an application associated to the shortcut.
        /// </summary>
        /// <param name="shortcut">The shortcut.</param>
        public static void Execute(ShortcutData shortcut)
        {
            var path = shortcut.FullPath;
            if (!ShortcutValidator.ValidatePath(path))
            {
                ShortcutDal.Instance.Validate();
                throw new ApplicationException("The full path to the directory or file is invalid.");
            }
            
            new Thread(() => ExecuteOnThread(shortcut.FullPath)).Start();
        }

        /// <summary>
        /// Executes an special command.
        /// </summary>
        /// <param name="specialFolder">The special folder command.</param>
        public static void Execute(Environment.SpecialFolder specialFolder)
        {
            var path = Environment.GetFolderPath(specialFolder);          
            new Thread(() => ExecuteOnThread(path)).Start();
        }

        /// <summary>
        /// Executes a file or folder.
        /// </summary>
        /// <param name="fullPath">The full path of the file or folder.</param>
        public static void Execute(string fullPath)
        {
            ValidatePath(fullPath);

            new Thread(() => ExecuteOnThread(fullPath)).Start();            
        }

        #endregion

        #region "Private Methods"

        /// <summary>
        /// Executes the shortcut on a separated thread.
        /// </summary>
        /// <param name="fullPath">The full path.</param>
        private static void ExecuteOnThread(string fullPath)
        {
            Process.Start(fullPath);
        }

        /// <summary>
        /// Checks if the directory or file exists.
        /// </summary>
        /// <param name="fullPath">The full path.</param>
        private static void ValidatePath(string fullPath)
        {
            if (!ShortcutValidator.ValidatePath(fullPath))
            {
                throw new ApplicationException("The full path to the directory or file is invalid.");                
            }            
        }

        #endregion
    }
}
